package com.cn.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq消息队列配置类
 *
 * @author:Wangjf
 * @date:2019年09月29日 下午3:49:10
 * @version 1.0
 */
@EnableRabbit
@Configuration
public class RabbitConfig {

    // 秒杀 start
    @Bean
    public Queue seckillQueue() {
        return QueueBuilder.durable(RabbitConst.SECKILL_QUEUE).build();
    }

    @Bean
    public DirectExchange seckillExchange() {
        return new DirectExchange(RabbitConst.SECKILL_EXCHANGE);
    }

    @Bean
    public Binding seckillNotifyBinding(Queue seckillQueue, DirectExchange seckillExchange) {
        return BindingBuilder.bind(seckillQueue).to(seckillExchange)
                .with(RabbitConst.SECKILL_ROUTING_KEY);
    }
    // 秒杀 end
    // 漏桶算法 start
    @Bean
    public Queue leakyBucketQueue() {
        return QueueBuilder.durable(RabbitConst.LEAKY_BUCKET_QUEUE).build();
    }

    @Bean
    public DirectExchange leakyBucketExchange() {
        return new DirectExchange(RabbitConst.LEAKY_BUCKET_EXCHANGE);
    }

    @Bean
    public Binding leakyBucketNotifyBinding(Queue leakyBucketQueue, DirectExchange leakyBucketExchange) {
        return BindingBuilder.bind(leakyBucketQueue).to(leakyBucketExchange)
                .with(RabbitConst.LEAKY_BUCKET_ROUTING_KEY);
    }
    // 漏桶算法 end
    /**
     * 接收消息转换
     *
     * @author: wangjf
     * @createTime: 2019/10/23 14:33
     * @param connectionFactory
     * @return org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        factory.setPrefetchCount(1);
        factory.setAutoStartup(true);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    /**
     * 发送消息转换
     * @author: wangjf
     * @createTime: 2019/10/23 14:34
     * @param connectionFactory
     * @return org.springframework.amqp.rabbit.core.RabbitTemplate
     */
	@Bean
	public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
		RabbitTemplate rabbitTemplate = new RabbitTemplate();
		rabbitTemplate.setConnectionFactory(connectionFactory);
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		return rabbitTemplate;
	}
}
